#coding:utf8
'''
Created on 2017/2/20
22:45
@author: Moon
qq : 550302989
'''

import sys,os,re
project_path = os.path.abspath('../')
sys.path.append(project_path)

import Feature

class UserManage(object):
    # 初始化
    def __init__(self):
        # 用反射获取类方法
        # self.Feature = __import__("Feature")
        # self.feature = getattr(self.Feature,"Feature")()
        self.__feature = Feature.Feature()

    # 验证用户和密码
    def checkUser(self,username,password):
        sql = "select id,avatar from users where name='{username}' and password='{password}'".format(username=username,password=password)
        return self.__feature.findFeatureOne(sql)
    # 验证用户是否已经被注册
    def checkUserName(self,username):
        sql = "select id from users where name='{username}'".format(username=username)
        return self.__feature.findFeatureOne(sql)

    # 查看用户所有信息
    def findUsersInfo(self):
        sql = "select users.id,users.name,password,qq,phone,email,auth.authname,users.avatar from users,auth where auth.id=users.authRank "
        return self.__feature.findFeatureAll(sql)

    # 根据用户名，查看用户所有信息 判断用户名是否存在
    def findUserInfoForName(self,username):
        sql = "select users.id,users.name,password,qq,phone,email,auth.authname,avatar from users,auth where auth.id=users.authRank and users.name='{username}'".format(username=username)
        return self.__feature.findFeatureOne(sql)

    # 添加用户
    def addUser(self,userinfo):
        sql = "insert into users(name,password,qq,email,phone,authrank) values('{name}','{password}','{qq}','{email}','{phone}','{authrank}')".format(
            name=userinfo['name'],password=userinfo['password'],qq=userinfo['qq'],email=userinfo['email'],phone=userinfo['phone'],authrank=userinfo['authrank']
        )
        return self.__feature.dataCommitFeature(sql)

    # 修改密码
    def updateUserPassword(self,userid,password):
        sql = "update users set password='{password}' where id='{userid}'".format(
            password=password,userid=userid
        )
        return self.__feature.dataCommitFeature(sql)

    # 修改用户信息
    def updateUser(self,userinfo):
        sql="update users set qq='{qq}',email='{email}',phone='{phone}' where users.id='{userid}'".format(
            qq=userinfo['qq'],email=userinfo['email'],phone=userinfo['phone'],userid=userinfo['userid']
        )
        return self.__feature.dataCommitFeature(sql)

    # 删除用户
    def deleteUser(self,userid):
        sql = "delete from users where name='{userid}'".format(userid=userid)
        return self.__feature.dataCommitFeature(sql)

    # 权限提升
    def upGradeAuth(self,authrank,userid):
        sql = "update users set authrank='{authrank}' where id='{userid}'".format(authrank=authrank,userid=userid)
        return self.__feature.dataCommitFeature(sql)

    # 添加权限
    def addAuth(self,authname):
        sql = "insert into auth(authname) values(authname='{authname}')".format(authname=authname)
        return self.__feature.dataCommitFeature(sql)

if __name__ == '__main__':
    userManage = UserManage()
    result = userManage.checkUser('meed','123123')
    print result
